package com.mive.dao.impl;

import com.mive.dao.AuthorDao;
import com.mive.entity.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class AuthorDaoImpl implements AuthorDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(Author author) {
        jdbcTemplate.update("insert into t_author(id ,real_name, nick_name) values (?, ?, ?)",
        author.getId(), author.getRealName(), author.getNickName());
    }

    @Override
    public void modify(Author author) {
        jdbcTemplate.update("update t_author set real_name = ?, nick_name = ? where id = ?",
                author.getRealName(), author.getNickName(), author.getId());
    }

    @Override
    public void romove(Long uid) {
        jdbcTemplate.update("delete from t_author where id = ?",uid);
    }

    @Override
    public Author getAutho(Long uid) {
        List<Author> list = jdbcTemplate.query("select id, real_name as realName, nick_name as nickName " +
                "from t_author where id = ?", new Object[]{uid}, new BeanPropertyRowMapper<>(Author.class));
        if (list.size()>0){
            Author author = list.get(0);
            return author;
        }else {
            return null;
        }
    }

    @Override
    public List<Author> getAuthorList() {
        List<Author> list = jdbcTemplate.query("select id, real_name as realName, nick_name as nickName " +
                "from t_author", new BeanPropertyRowMapper<>(Author.class));
        return list;
    }
}
